﻿var n1 = 0, CS_CellNum = 66, g1;
var txtTie2 = null, LNR, LNC,
    c2 = 0, r2 = 0, CS_ColNum2 = 39, arrData2, g2;
var c3 = 0, r3 = 0, CS_ColNum3 = 78, arrData3, g3;
var c4 = 0, r4 = 0, CS_ColNum4 = 38, arrData4, g4;
var c5 = 0, r5 = 0, CS_ColNum5 = 38, arrData5, g5;

window.onload = function () {
    g1 = document.getElementById("g1");
    g2 = document.getElementById("g2");
    g3 = document.getElementById("g3");
    g4 = document.getElementById("g4");
    g5 = document.getElementById("g5");
    arrData2 = CreateArrData(CS_ColNum2);
    arrData3 = CreateArrData(CS_ColNum3);
    arrData4 = CreateArrData(CS_ColNum4);
    arrData5 = CreateArrData(CS_ColNum5);
}

function CreateArrData(colNum) {
    var arrData = new Array(6);
    for (var i = 0; i < 6; i++) {
        arrData[i] = new Array(colNum);
        for (var j = 0; j < colNum; j++) {
            arrData[i][j] = 0;
        }
    }
    return arrData;
}

function ClearArrData(arrData, colNum) {
    for (var i = 0; i < 6; i++) {
        for (var j = 0; j < colNum; j++) {
            arrData[i][j] = 0;
        }
    }
}

function GetNextCell(arrData, colNum, row, col, result) {
    if ((arrData[row][col] > 0) ^ (result > 0)) {
        col = 1;
        while (arrData[0][col] != 0) {
            if (++col == colNum) return;
        }
        row = 0;
    }
    else if (row == 5) {
        if (++col == colNum) return;
    }
    else if (arrData[row + 1][col] != 0) {
        if (++col == colNum) return;
    }
    else
        row++;
    return [row, col];
}

function DrawT1(r, b, p) {
    if (n1 >= CS_CellNum) return;
    var cx = 20 + 30 * Math.floor(n1 / 6);
    var cy = 20 + 30 * (n1 % 6);
    n1++;
    var color = "green";
    var text = "和";
    if (r > 0) {
        color = "red";
        text = "庄";
    } else if (r < 0) {
        color = "blue";
        text = "闲";
    }
    var cObj = document.createElementNS("http://www.w3.org/2000/svg", "circle");
    cObj.setAttribute("cx", cx);
    cObj.setAttribute("cy", cy);
    cObj.setAttribute("r", 14);
    cObj.setAttribute("fill", color);
    g1.appendChild(cObj);

    var tObj = document.createElementNS("http://www.w3.org/2000/svg", "text");
    tObj.setAttribute("x", cx);
    tObj.setAttribute("y", cy + 2);
    tObj.setAttribute("fill", "white");
    tObj.setAttribute("style", "font-size:20;font-weight:bold;text-anchor: middle;dominant-baseline: middle;");
    tObj.innerHTML = text;
    g1.appendChild(tObj);

    if (b) {
        cObj = document.createElementNS("http://www.w3.org/2000/svg", "circle");
        cObj.setAttribute("cx", cx - 10);
        cObj.setAttribute("cy", cy - 10);
        cObj.setAttribute("r", 4);
        cObj.setAttribute("fill", "red");
        cObj.setAttribute("stroke", "white");
        cObj.setAttribute("stroke-width", 1);
        g1.appendChild(cObj);
    }

    if (p) {
        cObj = document.createElementNS("http://www.w3.org/2000/svg", "circle");
        cObj.setAttribute("cx", cx + 10);
        cObj.setAttribute("cy", cy + 10);
        cObj.setAttribute("r", 4);
        cObj.setAttribute("fill", "blue");
        cObj.setAttribute("stroke", "white");
        cObj.setAttribute("stroke-width", 1);
        g1.appendChild(cObj);
    }
}

function DrawT2(r, b, p) {
    if (r == 0) {
        if (txtTie2 == null) {
            var cx = 352 + 14 * c2;
            var cy = 12 + 14 * r2;
            txtTie2 = document.createElementNS("http://www.w3.org/2000/svg", "text");
            txtTie2.setAttribute("x", cx);
            txtTie2.setAttribute("y", cy);
            txtTie2.setAttribute("fill", "green");
            txtTie2.setAttribute("style", "font-size:10;text-anchor: middle;dominant-baseline: middle;");
            txtTie2.innerHTML = "1";
            g2.appendChild(txtTie2);

            var lObj = document.createElementNS("http://www.w3.org/2000/svg", "line");
            lObj.setAttribute("x1", cx - 2);
            lObj.setAttribute("x2", cx + 5);
            lObj.setAttribute("y1", cy + 6);
            lObj.setAttribute("y2", cy - 1);
            lObj.setAttribute("stroke", "green");
            lObj.setAttribute("stroke-width", 0.5);
            lObj.setAttribute("shape-rendering", "crispEdges");
            g2.appendChild(lObj);
        } else {
            var _hNum = txtTie2.innerHTML;
            if (_hNum >= 9) return;
            txtTie2.innerHTML = ++_hNum;
        }
    } else {
        if (c2 > 0 || r2 > 0 || arrData2[0][0] != 0) {
            txtTie2 = null;
            var _v = GetNextCell(arrData2, CS_ColNum2, r2, c2, r)
            if (_v == undefined) return;
            r2 = _v[0];
            c2 = _v[1];
            //update the LNR,LNC for T3,T4,T5
            LNC = c2;
            if (r2 == 0) { //new column
                LNC--;
                for (var i = 6; i > 1; i--) {
                    if (arrData2[i - 1][LNC] != 0) break;
                }
                LNR = i;
            }
            else
                LNR = r2;
        }
        arrData2[r2][c2] = r;
        var cx = 352 + 14 * c2;
        var cy = 12 + 14 * r2;
        var cObj = document.createElementNS("http://www.w3.org/2000/svg", "circle");
        cObj.setAttribute("cx", cx);
        cObj.setAttribute("cy", cy);
        cObj.setAttribute("r", 6);
        cObj.setAttribute("fill", "none");
        if (r > 0)
            cObj.setAttribute("stroke", "red");
        else
            cObj.setAttribute("stroke", "blue");
        g2.appendChild(cObj);

        if (b) {
            cObj = document.createElementNS("http://www.w3.org/2000/svg", "circle");
            cObj.setAttribute("cx", cx - 4);
            cObj.setAttribute("cy", cy - 4);
            cObj.setAttribute("r", 1.5);
            cObj.setAttribute("fill", "red");
            cObj.setAttribute("stroke", "white");
            cObj.setAttribute("stroke-width", 0.5);
            g2.appendChild(cObj);
        }

        if (p) {
            cObj = document.createElementNS("http://www.w3.org/2000/svg", "circle");
            cObj.setAttribute("cx", cx + 4);
            cObj.setAttribute("cy", cy + 4);
            cObj.setAttribute("r", 1.5);
            cObj.setAttribute("fill", "blue");
            cObj.setAttribute("stroke", "white");
            cObj.setAttribute("stroke-width", 0.5);
            g2.appendChild(cObj);
        }

        if (c2 > 1 || (r2 > 0 && c2 > 0)) DrawT3();
        if (c2 > 2 || (r2 > 0 && c2 > 1)) DrawT4();
        if (c2 > 3 || (r2 > 0 && c2 > 2)) DrawT5();
    }
}

function GetNewColor(t) {
    var _c = LNC - t;
    var _p = 0;
    for (var i = 0; i < 6; i++) {
        if (arrData2[i][_c] != 0) _p++;
    }
    if ((_p == LNR) ^ (r2 == 0))
        return -1;
    else
        return 1;
}

function DrawT3() {
    var _clr = GetNewColor(1);
    if (c3 > 0 || r3 > 0 || arrData3[0][0] != 0) {
        var _v = GetNextCell(arrData3, CS_ColNum3, r3, c3, _clr)
        if (_v == undefined) return;
        r3 = _v[0];
        c3 = _v[1];
    }
    arrData3[r3][c3] = _clr;
    var cObj = document.createElementNS("http://www.w3.org/2000/svg", "circle");
    cObj.setAttribute("cx", 348.5 + 7 * c3);
    cObj.setAttribute("cy", 104.5 + 7 * r3);
    cObj.setAttribute("r", 2.8);
    cObj.setAttribute("fill", "none");
    if (_clr > 0)
        cObj.setAttribute("stroke", "red");
    else
        cObj.setAttribute("stroke", "blue");
    g3.appendChild(cObj);
}

function DrawT4() {
    var _clr = GetNewColor(2);
    if (c4 > 0 || r4 > 0 || arrData4[0][0] != 0) {
        var _v = GetNextCell(arrData4, CS_ColNum4, r4, c4, _clr)
        if (_v == undefined) return;
        r4 = _v[0];
        c4 = _v[1];
    }
    arrData4[r4][c4] = _clr;
    var cObj = document.createElementNS("http://www.w3.org/2000/svg", "circle");
    cObj.setAttribute("cx", 348.5 + 7 * c4);
    cObj.setAttribute("cy", 146.5 + 7 * r4);
    cObj.setAttribute("r", 2.9);
    if (_clr > 0)
        cObj.setAttribute("fill", "red");
    else
        cObj.setAttribute("fill", "blue");
    g4.appendChild(cObj);
}

function DrawT5() {
    var _clr = GetNewColor(3);
    if (c5 > 0 || r5 > 0 || arrData5[0][0] != 0) {
        var _v = GetNextCell(arrData5, CS_ColNum5, r5, c5, _clr)
        if (_v == undefined) return;
        r5 = _v[0];
        c5 = _v[1];
    }
    arrData5[r5][c5] = _clr;

    var dc = 7 * c5;
    var dr = 7 * r5;
    var lObj = document.createElementNS("http://www.w3.org/2000/svg", "line");
    lObj.setAttribute("x1", 626 + dc);
    lObj.setAttribute("y1", 149 + dr);
    lObj.setAttribute("x2", 631 + dc);
    lObj.setAttribute("y2", 144 + dr);
    if (_clr > 0)
        lObj.setAttribute("stroke", "red");
    else
        lObj.setAttribute("stroke", "blue");
    g5.appendChild(lObj);
}

function AddRecord(Result, BPair, PPair) {
    DrawT1(Result, BPair, PPair);
    DrawT2(Result, BPair, PPair);
}

function AddRecord2() {
    var Result = Math.floor(Math.random() * 3) - 1;
    var BPair = Math.random() > 0.495;
    var PPair = Math.random() > 0.495;
    DrawT1(Result, BPair, PPair);
    DrawT2(Result, BPair, PPair);
}

function Reset() {
    g1.innerHTML = "";
    g2.innerHTML = "";
    g3.innerHTML = "";
    g4.innerHTML = "";
    g5.innerHTML = "";
    ClearArrData(arrData2, CS_ColNum2);
    ClearArrData(arrData3, CS_ColNum3);
    ClearArrData(arrData4, CS_ColNum4);
    ClearArrData(arrData5, CS_ColNum5);
    n1 = c2 = r2 = c3 = r3 = c4 = r4 = c5 = r5 = 0;
    txtTie2 = null;
}

function Resize(width, height) {
    var mySvg = document.getElementById("tu2svg");
    mySvg.setAttribute("width", width);
    mySvg.setAttribute("height", height);
}